package com.shinewing.sconlyoffice.service;

import com.shinewing.sconlyoffice.dao.AttachmentDao;
import com.shinewing.sconlyoffice.helpers.FileIOHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.Map;
import java.util.Objects;

@Service
public class AttachmentService {

    private static final Logger log = LoggerFactory.getLogger(AttachmentService.class);
    @Autowired
    private AttachmentDao attachmentDao;

    @Value("${sc.file.path}")
    private String scFilePath;

    public boolean toSaveFile(String fileName, File SourceFile) {
        Map<String, Object> map = attachmentDao.findByDocId(fileName);
        if(Objects.isNull(map)){
            log.error("在数据库中没能查到"+fileName);
        }
        String fullpath = Objects.toString(map.get("SCATTACHMENT_FULLPATH"));
        boolean copy = FileIOHelper.copy(SourceFile, new File(scFilePath + fullpath));
        if ( copy){
            log.info("保存到"+fullpath);
        } else {
            log.error("保存失败到"+fullpath);
        }
        return copy;
    }

}
